package com.yxh.www.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxh.www.author.domain.SmServerApi;
import com.yxh.www.author.mapper.SmServerApiMapper;
import com.yxh.www.author.service.SmServerApiService;
import com.yxh.www.common.constant.Constant;
import com.yxh.www.common.result.Result;
import com.yxh.www.common.result.ResultBuilder;
import com.yxh.www.mysql.base.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统接口资源表 服务实现类
 * </p>
 *
 * @author Yangxiaohui
 * @since 2020-05-09
 */
@Service
@Transactional
public class SmServerApiServiceImpl extends BaseServiceImpl<SmServerApiMapper, SmServerApi> implements SmServerApiService {

    @Override
    public Integer countSmServerApiByServerId(String serverId) {
        return baseMapper.selectCount(new QueryWrapper<SmServerApi>().eq("server_id", serverId));
    }



    @Override
    public void updateSmServerApiByServerId(String serverId, List<SmServerApi> smServerApis) {
        // 查看该服务在数据库是否已经存在API信息
        if (this.countSmServerApiByServerId(serverId)>0){
            // 删除旧的数据
            baseMapper.delete(new QueryWrapper<SmServerApi>().eq("server_id", serverId));
        }
        baseMapper.insertList(smServerApis);
    }

    @Override
    public Set<String> listApiHash() {
        return baseMapper.listApiHash();
    }

    /**
     * 根据第三方账户ID 获取可访问接口列表
     *
     * @param smAccountId 第三方账户ID
     * @return 统一响应 {@link Result <List>} 实体类型 {@link SmServerApi}
     */
    @Override
    public Result<List<SmServerApi>> listSmServerApiBySmAccountId(String smAccountId) {
        return ResultBuilder.success(baseMapper.listSmServerApiBySmAccountId(smAccountId));
    }

    /**
     * 更新Api接口信息到Redis服务
     */
    @Override
    public void updateServerApiToRedis(RedisTemplate<String,Object> redisTemplate) {
        // 从数据库查询 数据
        List<SmServerApi> serverApiSet = baseMapper.listApiSetByUrlType(0);
        List<SmServerApi> serverPathValueApiSet = baseMapper.listApiSetByUrlType(1);
        // 更新或保存到Redis
        if (serverApiSet!=null&&!serverApiSet.isEmpty()) {
            // 常规API
            Map<String, Collection<String>> serverApiMap = serverApiSet.stream().collect(Collectors.toMap(SmServerApi::getApiPath, serverApi -> {
                String permissionKeys=serverApi.getPermissionKeys();
                return StringUtils.isBlank(permissionKeys)?new ArrayList<>():Arrays.asList(permissionKeys.split(","));
            }));
            redisTemplate.opsForHash().putAll(Constant.REDIS_SM_SERVER_API_KEY, serverApiMap);
        }
        if (serverPathValueApiSet!=null&&!serverPathValueApiSet.isEmpty()) {
            // 包含PathValue的API
            Map<String, List<String>> serverPathValueApiMap = serverPathValueApiSet.stream().collect(Collectors.toMap(SmServerApi::getApiPath, serverApi -> {
                String permissionKeys=serverApi.getPermissionKeys();
                return StringUtils.isBlank(permissionKeys)?new ArrayList<>():Arrays.asList(permissionKeys.split(","));
            }));
            redisTemplate.opsForHash().putAll(Constant.REDIS_SM_SERVER_PATH_VALUE_API_KEY, serverPathValueApiMap);
        }
    }


}
